<?php
    require_once "weixin_info.php";//微信授权获取用户头像等信息
    require_once "config.php";
    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        $params = array(
            'u_id'          => $_SESSION['Member_info'][0]['id'],
            'goods_id'      => $_POST['goods_id'],
            'shop_id'       => $_POST['shop_id'],
            'money'         => $_POST['money'],
            'number'        => $_POST['number'],
            'mobile'        => $_POST['mobile'],
            'remarks'       => $_POST['remarks'],
            'start_time'    => $_POST['start_time'],
            'end_time'      => $_POST['end_time'],
            'time'          =>   time(),
            'order_type'    => $_POST['order_type'],
            'guide_uid'     =>$_POST['guide_uid'],
            'youji'    => $_POST['youji'],
            'link_man'    => $_POST['link_man'],
            'link_address'    => $_POST['link_address'],
            'song'    => $param['song'],
        );

        $goods_sql = $mysql->query("select * from dyspw_goods where id=".$params['goods_id']);
        $goods = $goods_sql->fetch_all(MYSQLI_ASSOC);

        if($goods[0]['rob'] == 'rob'){
			$time = strtotime(date('Y-m-d'));
            $rob_sql = $mysql->query("select * from dyspw_goods_order where u_id={$_SESSION['Member_info'][0]['id']} and rob != '' and state != 0 and time > {$time}" );
            $rob = $rob_sql->fetch_all(MYSQLI_ASSOC);
            if(!empty($rob)){
                $result['status'] = 'n';
                $result['info'] = '您已购买过特价商品';
                exit(json_encode($result));
            }

        }

        if(($goods[0]['stock'] - $goods[0]['sales']) <= 0){
            $result['status'] = 'n';
            $result['info'] = '商品已售罄';
            exit(json_encode($result));
        }
        $shop_info = $mysql->query("select * from dyspw_member where id=".$params['shop_id']);
        $shop = $shop_info->fetch_all(MYSQLI_ASSOC);
        $waimai_info = $mysql->query("select * from dyspw_shop_waimai where uid=".$shop[0]['id']);
        $waimai = $waimai_info->fetch_all(MYSQLI_ASSOC);
        $params['u_avatar'] = $shop[0]['avatar'];
        $params['u_name'] = $shop[0]['username'];
        $params['Price'] = $goods[0]['group_money'];
        $params['ordernum'] = mt_rand(10,100).time().mt_rand(10,1000);
        $Totalprice = $goods[0]['group_money']*$_POST['number'];
        if($params['order_type'] == 'waimai'){
            $params['add_id'] = $_POST['add_id'];
            $params['meal_num'] = $_POST['meal_num'];
            $params['mobile_is'] = $_POST['mobile_is'];
            $params['waimai_id'] = $waimai[0]['id'];
            $params['reckon_match'] = $_POST['reckon_match'];
            $params['reckon_packing'] = $_POST['reckon_packing'];
            $Totalprice = $goods[0]['group_money'] + $params['reckon_match'] + $params['reckon_packing'];//总价
        }
        //触发微信支付
        require_once "WxPay.php";
        $WxPay = new WxPay($_SESSION['Member_info'][0]['openid']);
        // $Result = $WxPay->SubmitJsApiUnifiedOrder('吉事多', $params['ordernum'], $Totalprice);
        // print_r($shop);exit;
        $profit_sharing = !empty($shop[0]['merchant_num']) ? 'Y' : 'N';
        if($profit_sharing == 'N'){
            // exit('3');
            $mch_id = !empty($shop[0]['merchant_num']) ? $shop[0]['merchant_num'] : '';
            $mch_key = !empty($shop[0]['merchant_key']) ? $shop[0]['merchant_key'] : '';
            $Result = $WxPay->SubmitJsApiUnifiedOrder('吉事多', $params['ordernum'], $Totalprice);
            $SignParam = $WxPay->signJsApiPrepayParam($Result['prepay_id']);
        }else{
            // exit('4');
            require_once(dirname(__FILE__).'/include/common.inc.php');
            $wxPayment= new wxPayment($globalConfig['wx']);
            //服务商支付
            $orderCode=time().mt_rand(1000,9999);
            $result=$wxPayment->paymentProvider([
                'sub_mch_id'=>$shop[0]['merchant_num'],
                'profit_sharing'=>'Y',
                'body'=>'吉事多',
                'out_trade_no'=>$params['ordernum'],
                'total_fee'=> $Totalprice*100,
//                'total_fee'=> 1,
                'notify_url'=>'http://5p8.fyh5p8.com/xiaochengxu/WxPay_back.php',
                'trade_type'=>'JSAPI',
                'openid'=> $_SESSION['Member_info'][0]['openid']
            ]);
            if($result['status']){
                $SignParam=$result['data'];
            }else{
                $result['status'] = 'n';
                $result['info'] = '支付订单创建失败，请联系管理员';
                exit(json_encode($result));
            }
        }
        // print_r($SignParam);exit;
        if($params['order_type'] == 'waimai'){
            $or_sql = "INSERT INTO dyspw_goods_order(u_id, goods_id, shop_id, `money` ,`number`,mobile,remarks,start_time, end_time,`time`,
u_avatar,u_name,Price,ordernum,add_id,meal_num,mobile_is,waimai_id,order_type,profit_sharing,pack_fee,delivery_fee,Totalprice,guide_uid,link_address,link_man,youji,song)
            VALUES ('{$params['u_id']}','{$params['goods_id']}','{$params['shop_id']}','{$Totalprice}','{$params['number']}','{$params['mobile']}','{$params['remarks']}',
                '{$params['start_time']}','{$params['end_time']}','{$params['time']}','{$params['u_avatar']}','{$params['u_name']}','{$params['Price']}','{$params['ordernum']}',
                '{$params['add_id']}','{$params['meal_num']}','{$params['mobile_is']}','{$params['waimai_id']}','{$params['order_type']}','{$profit_sharing}','{$params['reckon_packing']}','{$params['reckon_match']}','{$Totalprice}','{$params['guide_uid']}','{$params['link_address']}','{$params['link_man']}','{$params['youji']}','{$params['song']}')";
        }else{
            $or_sql = "INSERT INTO dyspw_goods_order(u_id, goods_id, shop_id, `money` ,`number`,mobile,remarks,start_time, end_time,`time`,u_avatar,u_name,Price,ordernum,profit_sharing,guide_uid,link_address,link_man,youji,song,rob)
            VALUES ('{$params['u_id']}','{$params['goods_id']}','{$params['shop_id']}','{$params['money']}','{$params['number']}','{$params['mobile']}','{$params['remarks']}',
                '{$params['start_time']}','{$params['end_time']}','{$params['time']}','{$params['u_avatar']}','{$params['u_name']}','{$params['Price']}','{$params['ordernum']}','{$profit_sharing}','{$params['guide_uid']}','{$params['link_address']}','{$params['link_man']}','{$params['youji']}','{$params['song']}','{$goods[0]['rob']}')";
        }
        $add_order = mysqli_query($mysql, $or_sql);
        if(!empty($add_order)){
            $result['status'] = 'y';
    		$result['data'] = $SignParam;
            exit(json_encode($result));
        }else{
            $result['status'] = 'n';
            $result['info'] = '表单提交失败，请重新提交';
            exit(json_encode($result));
        }

    }
 ?>
